`
https://leetcode.cn/problems/cousins-in-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} x
 * @param {number} y
 * @return {boolean}
 */
var isCousins = function (root, x, y) {
  let xParent = null, yParent = null
  let xDepth = 0, yDepth = 0

  const traverse = (root, depth, parent) => {
    if (root === null) return

    if (root.val === x) {
      // 找到 x，记录它的深度和父节点
      xParent = parent;
      xDepth = depth;
    }
    if (root.val === y) {
      // 找到 y，记录它的深度和父节点
      yParent = parent;
      yDepth = depth;
    }

    const leftVal = traverse(root.left, depth + 1, root)
    const rightVal = traverse(root.right, depth + 1, root)
  }

  traverse(root, 0, null)

  return xParent !== yParent && xDepth === yDepth
};